<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 * @property Role $Role
 */
class User extends AppModel {

/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'user_id';


  public $validate = array(
        
        'username' => array( 
            'username_have_a_name' => array( 
            'rule' => array('notempty'), 
            'message' => 'Please enter a username'), 
       ),
//       'pwd' => array(
//            'password_must_be_entered' => array(
//            'rule' => array('notempty'),
//            'message' => 'Please enter a password'),
//            
//            'password_length' => array(
//            'rule'    => array('minLength', '6'),
//            'message' => 'Minimum 6 characters long'),
//        ),
        'staff_name' => array( 
            'staff_must_have_a_name' => array( 
            'rule' => array('notempty'), 
            'message' => 'Please enter a staff name'),
        ),  
    ); 


/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'username';

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Role' => array(
			'className' => 'Role',
			'foreignKey' => 'role_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
        
    public $hasAndBelongsToMany = array(
        'Roster' =>array(
            'className' => 'Roster',
            'jointTable' => 'rosters_users',
            'foreignKey' =>'user_id',
            'associationForeignKey' => 'roster_id'
            )  
    );
//        ),
//      
//        'Staffbook' =>array(
//            'className' => 'Staffbook',
//            'jointTable' => 'users_staffbook',
//            'foreignKey' =>'user_id',
//            'associationForeignKey' => 'staffbook_id'
         
       
//        public function beforeSave($options = array()) {
//            parent::beforeSave($options);
//            if(isset($this->data[$this->alias]['password'])){
//                $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);            
//            }
//            return true;
//        }
        
        var $actsAs = array('Acl' => array('requester'));
 
    function parentNode() {
        if (!$this->id && empty($this->data)) {
            return null;
        }
        $data = $this->data;
        if (empty($this->data)) {
            $data = $this->read();
        }
        if (!$data['User']['role_id']) {
            return null;
        } else {
            return array('Role' => array('role_id' => $data['User']['role_id']));
        }
    }
}
